import React, {useImperativeHandle, useState} from "react";
import {View, Button} from "@tarojs/components";
import { refType } from "@/types"

interface propsType {
  showProps: (arg0: Number) => void;
}

const childCom = React.forwardRef<refType, propsType>(({showProps}, ref) => {
  const [refVal, setRefVal] = useState(0)
  useImperativeHandle(ref, () => {
    return {
      id: 0,
      name: 'childCom',
      value: refVal,
      valPlus
    }
  }, [refVal])
  const valPlus = () => {
    setRefVal(val => val + 1)
  }

  return(
    <View>
      <View className="row">ref子组件值：{refVal}</View>
      <Button className="btn" size="mini" onClick={valPlus}>+1</Button>
      <Button className="btn" size="mini" onClick={() => showProps(refVal)}>调用props方法</Button>
    </View>
  )
})
export default childCom
